import React, { useImperativeHandle } from "react";
import { Form, Input, InputNumber, Radio } from 'antd';

import type { CreateOrEditActionRef } from "@yaf/pro-ui";
import { getEnv  } from "@/constants";

const layout = {
  labelCol: { span: 6 },
  wrapperCol: { span: 16 },
};

const CreatePost = React.forwardRef<CreateOrEditActionRef, { rowKey: string | number, values: any }>((props, actionRef) => {
  const [form] = Form.useForm();
  const { commonStatus, commonStatusList } = getEnv().globalConfig;

  useImperativeHandle(actionRef, () => ({
    getFormData: () => new Promise((resolve, reject) => {
      form.validateFields().then((data) => {
        resolve(data);
      }).catch(() => {
        reject(new Error("请检查参数"));
      });
    })
  }));

  return (
    <Form
      initialValues={{
        status: commonStatus.normal,
        postSort: 0,
        ...props.values
      }}
      form={form}
      {...layout}
    >
      <Form.Item
        label="岗位编码"
        name="postCode"
        rules={[{ required: true, message: '岗位编码!' }, { max: 30, message: '岗位编码不能超过30个字！' }]}
      >
        <Input placeholder="请输入岗位编码" />
      </Form.Item>
      <Form.Item
        label="岗位名称"
        name="postName"
        rules={[{ required: true, message: '岗位名称!' }, { max: 30, message: '岗位名称不能超过30个字！' }]}
      >
        <Input placeholder="请输入岗位名称" />
      </Form.Item>
      <Form.Item
        label="岗位排序"
        name="postSort"
      // rules={[{
      //   required: true,
      //   pattern: /[0-9]/,
      //   message: "请输入正确的序号数字！"
      // }, { max: 9, message: '数字不能超过9位！' }]}
      >
        <InputNumber placeholder="请输入序号" min={0} />
      </Form.Item>
      <Form.Item
        label="状态"
        name="status"
      // rules={[{ required: true, message: '情选择岗位状态!' }]}
      >
        <Radio.Group options={commonStatusList} />
      </Form.Item>
      <Form.Item
        label="备注"
        name="remark"
        rules={[{ max: 150, message: '备注信息不能超过150个字！' }]}
      >
        <Input.TextArea placeholder="请输入内容" />
      </Form.Item>
    </Form>
  )
});

export default CreatePost;
